<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Comment extends CI_Controller
{
    public $_module_name = "";
    public $pre_message="";
	function __construct()
	{
        parent::__construct();	
        $this->_module_name = $this->router->fetch_module();
		
		$this->load->config('config_option') ;
	  	$this->session->set_userdata(array('Url'=>uri_string()));   
        $this->lang->load('news');
		$this->load->model('comment_model','comment');
	}

	function index($kind=0, $current_page=0)
	{
        //$this->acl->check('view','','',base_url());
        
		
        //if($this->acl->check('add')) 
        $data['add'] = $this->_module_name.'/comment/add';
		$data = array();
		$data['title'] = lang('comment.list');
        
        $config['base_url'] = base_url().$this->_module_name.'/comment/index/'.$kind;
		$config['total_rows']   =  $this->comment->get_num_comment();
		$data['num'] = $config['total_rows'];
		$config['per_page']  =  20;//$this->config->item('per_page');
		$config['uri_segment'] = $this->uri->total_segments();
		$this->pagination->initialize($config);
        $data['pagination']    = $this->pagination->create_links();
        
		$this->load->helper('str');
		$data['list'] =   $this->comment->get_all_comment($config['per_page'],$current_page, $kind);
        $data['current_kind']=$kind;
		$data['current_page']=$current_page;
		$this->_templates['page'] = 'comment/comment_list';
		$this->site->load($this->_templates['page'],$data);
	}

	function view($id)
	{
		$data = array();
		$data['title'] = lang('update');
        
		//$data['zone'] = $this->comment->get_list_zone(0);
		//$this->form_validation->set_rules('lb_content',lang('name_category'),'trim|required');
		$data['rs'] = $this->comment->get_comment($id);
		
		$this->_templates['page'] = 'comment/comment_view';
		$this->site->load($this->_templates['page'],$data, 'basic');
		//$this->load->view($this->_templates['page'], $data);
	}  
	/**
	@author binh.ngo
	@date create 10/4/2012
	@method load page edit category
	@return void;
	**/
	function add()
	{
        $this->acl->check('add','','',base_url());
        
		  $data = array();
		  $data['title'] = lang('add_news');
		  $data['zone'] = $this->comment->get_list_zone(0);
		  $this->form_validation->set_rules('lb_content',lang('name_category'),'trim|required');
		  if($this->form_validation->run()== FALSE){
			  $this->pre_message = validation_errors();
		  }else{
		  		$data =  $this-> build_data($_POST,1); 
			  	if($this->comment->saveData(0,$data)){
				                     
				  $this->session->set_flashdata('message',lang('admin.save_successful'));
				  redirect($this->_module_name.'/comment');
			  }else{
                  $this->pre_message = lang('admin.save_unsuccessful');
              }
		  }
	
		  $data['message'] = $this->pre_message;
		
		$this->_templates['page'] = 'comment/comment_add_view';
		$this->site_library->load($this->_templates['page'],$data);
	}  
	/**
	@author binh.ngo
	@date create 10/4/2012
	@method load   category
	@return void;
	**/
	function edit($id,$current_page=0)
	{
        $this->acl->check('edit','','',base_url());
        
		$data = array();
        
		$data['zone'] = $this->comment->get_list_zone(0);
		$data['rs'] = $this->comment->get_comment($id);
		$this->form_validation->set_rules('lb_content',lang('name_category'),'trim|required');
		
		if($this->form_validation->run()== FALSE){
			$this->pre_message = validation_errors();
		}else{
			$data =  $this->build_data($_POST,0); 
			if($this->comment->saveData($id,$data)){
				  $this->session->set_flashdata('message',lang('admin.save_successful'));
				  redirect($this->_module_name.'/comment/index/'.$current_page);
			}else{
                $this->pre_message = lang('admin.save_unsuccessful');
            }
		}
		$data['title'] = lang('update');
		$this->_templates['page'] = 'comment/comment_edit_view';
		$this->site_library->load($this->_templates['page'],$data);
	}  
	/**
	  @author binh.ngo
	  @date create 10/4/2012
	  @method build data for category
	  @return array;
	**/
	function build_data($flag=1)
	{
		$req["lb_content"] = $this->input->post('lb_content');
		$req["bl_active"] = (int) $this->input->post('bl_active');
		$req["cm_kind"] = (int) $this->input->post('cm_kind');
		if($flag==1){
			$req["dt_create"]= date("Y-m-d H:i:s");
		}
		return $req;
	}
  
	function trash($id,$current_page=0)
	{
        $this->acl->check('trash','','',base_url());
      
		if($this->comment->trash($id))
			$this->session->set_flashdata('message',lang('admin.delete_successful'));
		else $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));
		
		$this->comment->remove() ;
		redirect($this->_module_name.'/comment/index/'.$current_page);
  	}
	
	/**
	@author binh.ngo
	@date create 10/4/2012
	@method delete more category
	@return void;
	**/
	function trashes()
	{
        $this->acl->check('trash','','',base_url());
        
		if(!empty($_POST['ar_id']))
		{
			$current_page = (int)$this->input->post('current_page');
			$ar_id = $this->input->post('ar_id');
					
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) 
				{
					if ($ar_id[$i])
					{
						if($this->comment->trash($ar_id[$i]))
							$this->session->set_flashdata('message',lang('admin.delete_successful'));
						else $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));
					}
				}
			}
		}
		redirect($this->_module_name.'/comment/index/'.$current_page);
	}  
	
}

?>